#region Copyright
//Copyright 2008 PivDev

//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at

//   http://www.apache.org/licenses/LICENSE-2.0

//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License. 
#endregion

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using Microsoft.Win32;

namespace CRMAddin
{
    public partial class DSNChooser : Form
    {
        public string serverName = "";
        public string password = "";
        public string userName = "";
        public string DBEngine = "";

        public class DSNChoice
        {
            private string _DispName;
            private string _DBEngine;

            public override string ToString()
            {
                return _DispName;
            }
            public string DBEngine
            {
                get { return _DBEngine; }
                set { _DBEngine = value; }
            }

            public string DispName
            {
                get { return _DispName; }
                set { _DispName = value; }
            }

            public DSNChoice(string DisplayName, string DBEngine)
            {
                _DBEngine = DBEngine;
                _DispName = DisplayName;
            }
        }

        public DSNChooser()
        {
            InitializeComponent();
        }

        private void DSNChooser_Load(object sender, EventArgs e)
        {
            tbPassword.Enabled = false;

            #region Add System DSNs to List box
            populateDSN();


            #endregion


        }

        private void dsnListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            // string dsnName = (string)dsnListBox.SelectedItem;
            UpdateDSNInfo();

        }

        #region Utility Stuff


        private void populateDSN()
        {
            // Get System DNS Names
            RegistryKey reg = (Registry.LocalMachine).OpenSubKey("Software");
            reg = reg.OpenSubKey("ODBC");
            reg = reg.OpenSubKey("ODBC.INI");
            reg = reg.OpenSubKey("ODBC Data Sources");
            dsnListBox.Items.Clear();
            if (reg != null)
            {
                // Get all DSN entries defined in DSN_LOC_IN_REGISTRY.
                foreach (string s in reg.GetValueNames())
                {
                    try
                    {
                        dsnListBox.Items.Add(new DSNChoice(s, reg.GetValue(s).ToString()));
                    }
                    catch (System.Exception ex)
                    {
                        throw new Exception(ex.Message, ex);
                    }
                }
            }
            try
            {
                reg.Close();
            }
            catch (System.Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }


        private void UpdateDSNInfo()
        {
            tbPassword.Text = "";
            tbServer.Text = "";
            tbUserName.Text = "";
            RegistryKey reg = (Registry.LocalMachine).OpenSubKey("Software");
            reg = reg.OpenSubKey("ODBC");
            reg = reg.OpenSubKey("ODBC.INI");
            //reg = reg.OpenSubKey("ODBC Data Sources");
            DSNChoice dsnSelected = (DSNChoice)dsnListBox.SelectedItem;

            string dsnName = dsnSelected.DispName;
            reg = reg.OpenSubKey(dsnName);
            if (dsnSelected.DBEngine.Contains("SQL Server"))
            {
                tbServer.Text = reg.GetValue("Server").ToString();
                tbUserName.Text = reg.GetValue("Database").ToString();
                tbPassword.Enabled = false;
                lblUserName.Text = "Database";
                DBEngine = "SQL";
            }
            else if (dsnSelected.DBEngine.Contains("Oracle"))
            {
                //string serverName = reg.GetValue("ServerName").ToString();

                tbServer.Text = reg.GetValue("ServerName").ToString();
                tbUserName.Text = reg.GetValue("UserID").ToString();
                tbPassword.Enabled = true;
                DBEngine = "Oracle";
            }
        }
        #endregion

        private void Ok_Click(object sender, EventArgs e)
        {
            if (tbPassword.Text == "" && DBEngine == "Oracle")
            {
                MessageBox.Show("Please enter a password before connecting", "Enter Password", MessageBoxButtons.OK);
                return;
            }

            password = tbPassword.Text;
            userName = tbUserName.Text;
            serverName = tbServer.Text;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            serverName = "";
            password = "";
            userName = "";
            this.Close();
        }


    }
}